package landx.toolkit.utils
{
	import flash.display.Graphics;
	import flash.geom.Point;

	public class GraphicUtil
	{
		/**
		 *在图形上绘制虚线,颜色和线条粗细在lineStyle中设置
		 * @param graphic		绘制的图形
		 * @param fromx	起始X
		 * @param fromy	起始Y
		 * @param tox	目标X
		 * @param toy	目标Y
		 * @param lineLen	线条长度
		 */		
		public static function drawDotted(graphic:Graphics,fromx:Number,fromy:Number,tox:Number,toy:Number,lineLen:Number=5):void{
			var ps:Point=new Point(fromx,fromy);
			var pe:Point=new Point(tox,toy);
			var max:Number=Point.distance(ps,pe);
			//向量
			var pp:Point=CoordUtil.UnitPoint(ps,pe);
			pp.x*=lineLen;
			pp.y*=lineLen;
			var cout:Number=max/lineLen;
			for(var i:int=0;i<cout;i+=2){
				var sx:Number=ps.x+pp.x*i;
				var sy:Number=ps.y+pp.y*i;
				var cindex:Number=i+1;
				if(cindex>=cout){cindex=cout;}
				var tx:Number=ps.x+pp.x*cindex;
				var ty:Number=ps.y+pp.y*cindex;
				
				graphic.moveTo(sx,sy);
				graphic.lineTo(tx,ty);
			}
		}
		
		/**
		 * 绘制从起始点到目标点的直线 
		 * @param graphic		绘制的图形
		 * @param fromx	起始点x
		 * @param fromy	起始点y
		 * @param tox	目标点x
		 * @param toy	目标点y
		 */		
		public static function drawLine(graphic:Graphics,fromx:Number,fromy:Number,tox:Number,toy:Number):void{
			graphic.moveTo(fromx,fromy);
			graphic.lineTo(tox,toy);
		}
	}
}